/**
 * 
 */
package com.effectivsocial.extension.properties;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.factory.config.PropertiesFactoryBean;

/**
 * @author Dhrubo
 *
 */
public class ExtendedPropertiesFactoryBean extends PropertiesFactoryBean{

	private boolean propagateToSystemProperties = false;
	 
    public void setPropagateToSystemProperties(final boolean propagateToSystemProperties) {
        this.propagateToSystemProperties = propagateToSystemProperties;
    }
 
    @Override
    protected Properties createProperties() throws IOException {
        final Properties originalProperties = super.createProperties();
        final Properties propertiesReplaced = new Properties();
 
        final PropertyVariableParser propertyParser = new PropertyVariableParser();
 
        for (final Map.Entry<Object, Object> entry : originalProperties.entrySet()) {
            propertiesReplaced.put(
                entry.getKey(),
                propertyParser.replaceVariables((String) entry.getValue())
            );
        }
 
        if (propagateToSystemProperties) {
            final Properties systemProperties = System.getProperties();
            systemProperties.putAll(propertiesReplaced);
            System.setProperties(systemProperties);
        }
        return propertiesReplaced;
    }
}